﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using IMDTO;


namespace IMDAL
{
    public class IMDALNaturalPerson
    {
        public List<IMDTONaturalPerson> GetAllNaturalPersons()
        {
            try
            {

                // Init entity framework
                InMedioEntities entities = new InMedioEntities();

                //Get all persons
                List<NaturalPerson> persons = (from p in entities.NaturalPerson 
                                               where p.version == 0
                                               select p).ToList();

                List<IMDTONaturalPerson> IMDTOnpL = new List<IMDTONaturalPerson>();
                if (persons != null)
                {
                    foreach (NaturalPerson person in persons)
                    {
                        IMDTOnpL.Add(new IMDTONaturalPerson(person));
                    }
                }

                return IMDTOnpL;

            }
            catch (Exception e)
            {
              
                throw e;

            }

        }

        public List<IMDTONaturalPersonShort> GetNaturalPersonShortsByName(string filterName)
        {
            try
            {
                List<NaturalPerson> persons;
                // Init entity framework
                InMedioEntities entities = new InMedioEntities();
                if (String.IsNullOrEmpty(filterName) || String.IsNullOrWhiteSpace(filterName))
                {
                    //Get all persons
                    persons = (from p in entities.NaturalPerson
                               where p.version == 0
                                                   select p).ToList();
                }
                else
                {
                    //Get all persons search on name
                    persons = (from p in entities.NaturalPerson
                                                   where (p.Name.FirstName.ToLower().Contains(filterName.ToLower())
                                                   || p.Name.LastName.ToLower().Contains(filterName.ToLower())) && p.version == 0
                                                   select p).ToList();
                }

                List<IMDTONaturalPersonShort> IMDTOnpL = new List<IMDTONaturalPersonShort>();
                if (persons != null)
                {
                    foreach (NaturalPerson person in persons)
                    {
                        IMDTOnpL.Add(new IMDTONaturalPersonShort(person));
                    }
                }

                return IMDTOnpL;

            }
            catch (Exception e)
            {

                throw e;

            }

        }


        public IMDTONaturalPerson GetNaturalPersonForMembershipUser(Guid userGuid)
        {
            IMDTONaturalPerson naturalPerson;

            try
            {
                InMedioEntities entities = new InMedioEntities();

                NaturalPerson person = (from np in entities.NaturalPerson
                            where np.aspnet_Users.UserId.Equals(userGuid) && np.version == 0
                      select np).Single();
                naturalPerson = new IMDTONaturalPerson(person);

            }
            catch (Exception e)
            {
                throw e;
            }
            
            return naturalPerson;
        }

        
   


    }
}
